package cc.smarnet.caihou.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.io.Serializable;
import java.util.List;

import cc.smarnet.caihou.R;
import cc.smarnet.caihou.entity.Deliver;
import cc.smarnet.caihou.entity.DeliverVariety;
import cc.smarnet.caihou.phone.deliver.DeliverDetailActivity;
import cc.smarnet.caihou.phone.deliver.DeliverVarietyDetailActivity;


/**
* 出库品种信息的adapter
* */
public class DeliverVarietyAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<DeliverVariety> list;
    private Deliver deliver;//获取deliver的目的在于把deliver传到出库品种详情界面后，再返回到出库详情界面时需要

    public DeliverVarietyAdapter(Context context, List<DeliverVariety> list,Deliver deliver){
        this.context=context;
        this.list=list;
        this.deliver=deliver;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.deliver_variety_item,parent,false);
        deliverViewHolder deliverViewHolder=new deliverViewHolder(view);
        return deliverViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        DeliverVariety deliverVariety=list.get(position);
        deliverViewHolder deliverViewHolder= (deliverViewHolder) holder;
        deliverViewHolder.classes.setText(deliverVariety.getClasses());
        deliverViewHolder.flowerPoleLength.setText(deliverVariety.getFlowerPoleLength());
        deliverViewHolder.number.setText(deliverVariety.getNumber());
        deliverViewHolder.packingSpecifications.setText(deliverVariety.getPackingSpecifications());
        deliverViewHolder.variety.setText(deliverVariety.getVariety());
        deliverViewHolder.varietyCode.setText(deliverVariety.getVarietyCode());

    }

    @Override
    public int getItemCount() {
        // list.size()为0的话也必须至少返回1，返回有数值的话才会显示item，因为该方法不会返回0
        return list.size() > 0 ? list.size() : 1;
    }

    class deliverViewHolder extends RecyclerView.ViewHolder{
        TextView classes;
        TextView flowerPoleLength;
        TextView number;
        TextView packingSpecifications;
        TextView variety;
        TextView varietyCode;

        public deliverViewHolder(@NonNull View itemView) {
            super(itemView);
            classes=itemView.findViewById(R.id.deliver_variety_item_classes);
            flowerPoleLength=itemView.findViewById(R.id.deliver_variety_item_flowerPoleLength);
            number=itemView.findViewById(R.id.deliver_variety_item_number);
            packingSpecifications=itemView.findViewById(R.id.deliver_variety_item_packingSpecifications);
            variety=itemView.findViewById(R.id.deliver_variety_item_variety);
            varietyCode=itemView.findViewById(R.id.deliver_variety_item_varietyCode);

            // item点击事件
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DeliverVariety deliverVariety=list.get(getAdapterPosition());
                    Intent intent = new Intent(context, DeliverVarietyDetailActivity.class);
                    intent.putExtra("deliverDetail", (Serializable) deliver);
                    intent.putExtra("deliverVarietyDetail", (Serializable) deliverVariety);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
                    context.startActivity(intent);
                }
            });
        }
    }
}
